<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-title">
        <img src="@/assets/imgs/EFAM.png" alt="logo" />
        <span>邮箱快捷登录</span>
      </div>

      <el-form :model="form" :rules="rules" ref="formRef" @submit.native.prevent="login">
        <!-- 邮箱 -->
        <el-form-item prop="email">
          <el-input
              v-model="form.email"
              size="medium"
              prefix-icon="el-icon-message"
              clearable
              placeholder="请输入邮箱"
          />
        </el-form-item>

        <!-- 验证码 -->
        <el-form-item prop="code">
          <el-input
              v-model="form.code"
              size="medium"
              prefix-icon="el-icon-key"
              clearable
              placeholder="请输入验证码"
              maxlength="6"
          >
            <template #append>
              <el-button
                  style="width: 110px"
                  :disabled="coolDown > 0"
                  @click="sendCode"
              >
                {{ coolDown ? `${coolDown}s` : '发送验证码' }}
              </el-button>
            </template>
          </el-input>
        </el-form-item>

        <!-- 登录按钮 -->
        <el-form-item>
          <el-button
              type="primary"
              size="medium"
              style="width: 100%"
              :loading="loading"
              native-type="submit"
          >
            登 录
          </el-button>
        </el-form-item>
        <div style="display: flex; align-items: center">
          <div style="flex: 1"></div>
          <div style="flex: 1; text-align: right">
            <a href="/login">返回登录</a>
          </div>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'EmailLogin',
  data() {
    return {
      form: { email: '', code: '' },
      rules: {
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { type: 'email', message: '邮箱格式不正确', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { len: 6, message: '验证码为6位', trigger: 'blur' }
        ]
      },
      coolDown: 0,
      timer: null,
      loading: false
    }
  },
  methods: {
    async sendCode() {
      try {
        await this.$refs.formRef.validateField('email')
      } catch {
        return
      }
      await this.$request.post('/sendCode', { email: this.form.email }).then(res => {
        if (res.code === '200') {
          this.$message.success('验证码已发送')
          this.coolDown = 60
          this.timer = setInterval(() => {
            if (--this.coolDown === 0) clearInterval(this.timer)
          }, 1000)
        }
        else {
          this.$message.error(res.msg)
        }
      })
    },
    login() {
      this.$refs.formRef.validate(async valid => {
        if (!valid) return
        this.loading = true
        try {
          const res = await this.$request.post('/loginByEmail', this.form)
          if (res.code === '200') {
            localStorage.setItem('xm-user', JSON.stringify(res.data))
            await this.$router.push('/')
            this.$message.success('登录成功')
          } else {
            this.$message.error(res.msg || '登录失败')
          }
        } finally {
          this.loading = false
        }
      })
    }
  },
  beforeDestroy() {
    clearInterval(this.timer)
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  background: url(~@/assets/imgs/loginBack.png) center/cover no-repeat;
  display: flex;
  align-items: center;
  justify-content: center;
}
.login-card {
  width: 380px;
  padding: 40px 32px;
  background: rgba(255, 255, 255, 0.85);
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}
.login-title {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-bottom: 24px;
  font-size: 22px;
  color: #333;
}
.login-title img {
  height: 36px;
}
</style>